import com.xc.entity.User;
import com.xc.mapper.UserMapper;
import com.xc.utils.SqlSessionUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MybatisUserMapper {

    /**
     * 新增信息
     * @throws IOException
     */
    @Test
    public void test1() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
        SqlSession sqlSession = build.openSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int i = mapper.insertUser();
        sqlSession.commit();
        System.out.println("结果" + i);
        sqlSession.close();

    }

    /**
     * 修改信息
     */
    @Test
    public void test2(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int i = mapper.updateUser();
        sqlSession.commit();
        System.out.println("结果" + i);
        sqlSession.close();
    }
    /**
     * 删除信息
     */
    @Test
    public void test3(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int i = mapper.deleteUser();
        sqlSession.commit();
        System.out.println("结果" + i);
        sqlSession.close();
    }

    @Test
    public void test4(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryAllUser();
        sqlSession.commit();
        System.out.println("结果" +user );
        sqlSession.close();
    }

    /**
     * 根据姓名查询
     */
    @Test
    public void test6(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User 张三 = mapper.queryByName("张三");
        sqlSession.commit();
        System.out.println(张三);
        sqlSession.close();
    }

    /**
     * 根据查询
     */
    @Test
    public void test7(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryById(1);
        sqlSession.commit();
        System.out.println(user);
        sqlSession.close();
    }

    /**
     * 多个参数查询方式
     */
    @Test
    public void test8(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryByIdAndName(1, "张三");
        sqlSession.commit();
        System.out.println(user);
        sqlSession.close();

    }

    /**
     * 带Param查询
     */
    @Test
    public void test9(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.queryByParam(1);
        sqlSession.commit();
        System.out.println(user);
        sqlSession.close();

    }


    @Test
    public void test10(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> user = (List<User>) mapper.queryAllUser();
        sqlSession.commit();
        user.forEach(System.out::println);
        sqlSession.close();


    }

    @Test
    public void test11(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> list = mapper.testMohu("张");
        sqlSession.commit();
        list.forEach(System.out::println);
        sqlSession.close();

    }

    /**
     * 测试批量删除
     */
    @Test
    public void test12(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int i = mapper.deleteMore("3,4");
        sqlSession.commit();
        System.out.println(i);
        sqlSession.close();
    }

    /**
     * 自增主键
      */
//    @Test
//    public void test13(){
//        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
//        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//        int i = mapper.insertUser1(null);
//        sqlSession.commit();
//        System.out.println(i);
//        sqlSession.close();
//    }


    @Test
    public void test14(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = new User(null, "张三", 23, "男");
        List<User> list = mapper.dongTaiUser(user);
        sqlSession.commit();
        list.forEach(System.out::println);
        sqlSession.close();
    }


    @Test
    public void test15(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = new User(null, "李四", 18, "女");
        List<User> list = mapper.dongTaiUser1(user);
        sqlSession.commit();
        list.forEach(System.out::println);
        sqlSession.close();
    }


}
